<?php  if( !defined("IN_IA") ) 
{
	exit( "Access Denied" );
}
class Pay_EweiShopV2Page extends MobileLoginPage 
{
  
	public function main() 
	{
		global $_W;
		global $_GPC;
		$openid = $_W["openid"];
		$uniacid = $_W["uniacid"];
		$member = m("member")->getMember($openid, true);
		$orderid = intval($_GPC["id"]);
		$peerPaySwi = m("common")->getPluginset("sale");
		$peerPaySwi = $peerPaySwi["peerpay"]["open"];
		$ispeerpay = m("order")->checkpeerpay($orderid);
		if( !empty($order["istrade"]) ) 
		{
			$ispeerpay = 0;
		}
		if( !empty($ispeerpay) ) 
		{
			if( pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename("ewei_shop_order_peerpay_payinfo") . " WHERE openid = :openid AND pid = :pid", array( ":openid" => $_W["openid"], ":pid" => $ispeerpay["id"] )) ) 
			{
				$this->message("每人只能代付一次");
			}
			$peerpayMessage = trim($_GPC["peerpaymessage"]);
			$peerpay_info = (double) pdo_fetchcolumn("select SUM(price) from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $ispeerpay["id"] ));
			$peerprice = floatval($_GPC["peerprice"]);
			if( empty($peerprice) || $peerprice <= 0 ) 
			{
				header("Location:" . mobileUrl("order/pay/peerpayshare", array( "id" => $orderid )));
				exit();
			}
			$openid = pdo_fetchcolumn("SELECT openid FROM " . tablename("ewei_shop_order") . " WHERE id = :id AND uniacid = :uniacid LIMIT 1", array( ":id" => $orderid, ":uniacid" => $_W["uniacid"] ));
		}
		$order = pdo_fetch("select * from " . tablename("ewei_shop_order") . " where id=:id and uniacid=:uniacid and openid=:openid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid, ":openid" => $openid ));
		$og_array = m("order")->checkOrderGoods($orderid);
		if( !empty($og_array["flag"]) ) 
		{
			$this->message($og_array["msg"], "", "error");
		}
		if( empty($orderid) ) 
		{
			header("location: " . mobileUrl("order"));
			exit();
		}
		if( empty($order) ) 
		{
			header("location: " . mobileUrl("order"));
			exit();
		}
		$tradestatus = $order["tradestatus"];
		if( empty($order["istrade"]) ) 
		{
			if( $order["status"] == -1 ) 
			{
				header("location: " . mobileUrl("order/detail", array( "id" => $order["id"] )));
				exit();
			}
			if( 1 <= $order["status"] ) 
			{
				header("location: " . mobileUrl("order/detail", array( "id" => $order["id"] )));
				exit();
			}
		}
		else 
		{
			if( $order["status"] == 1 && $order["tradestatus"] == 1 ) 
			{
				$order["ordersn"] = $order["ordersn_trade"];
				$order["price"] = $order["betweenprice"];
			}
			else 
			{
				if( $order["status"] == 1 && $order["tradestatus"] == 2 ) 
				{
					header("location: " . mobileUrl("newstore/norder/detail", array( "id" => $order["id"] )));
					exit();
				}
				if( $order["status"] == 0 ) 
				{
					$order["price"] = $order["dowpayment"];
				}
			}
		}
		$log = pdo_fetch("SELECT * FROM " . tablename("core_paylog") . " WHERE `uniacid`=:uniacid AND `module`=:module AND `tid`=:tid limit 1", array( ":uniacid" => $uniacid, ":module" => "ewei_shopv2", ":tid" => $order["ordersn"] ));
		if( !empty($ispeerpay) ) 
		{
		}
		else 
		{
			if( !empty($log) && $log["status"] != "0" ) 
			{
				if( empty($order["istrade"]) ) 
				{
					header("location: " . mobileUrl("order/detail", array( "id" => $order["id"] )));
				}
				else 
				{
					header("location: " . mobileUrl("newstore/norder/detail", array( "id" => $order["id"] )));
				}
				exit();
			}
		}
		$seckill_goods = pdo_fetchall("select goodsid,optionid,seckill from  " . tablename("ewei_shop_order_goods") . " where orderid=:orderid and uniacid=:uniacid and seckill=1 ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $orderid ));
		if( !empty($log) && $log["status"] == "0" ) 
		{
			pdo_delete("core_paylog", array( "plid" => $log["plid"] ));
			$log = NULL;
		}
		if( empty($log) ) 
		{
			$log = array( "uniacid" => $uniacid, "openid" => trim($member["uid"]), "module" => "ewei_shopv2", "tid" => trim($order["ordersn"]), "fee" => $order["price"], "status" => 0 );
			pdo_insert("core_paylog", $log);
			$plid = pdo_insertid();
		}
		$set = m("common")->getSysset(array( "shop", "pay" ));
		$set["pay"]["weixin"] = (!empty($set["pay"]["weixin_sub"]) ? 1 : $set["pay"]["weixin"]);
		$set["pay"]["weixin_jie"] = (!empty($set["pay"]["weixin_jie_sub"]) ? 1 : $set["pay"]["weixin_jie"]);
		$param_title = $set["shop"]["name"] . "订单";
		$credit = array( "success" => false );
		if( isset($set["pay"]) && $set["pay"]["credit"] == 1 ) 
		{
			$credit = array( "success" => true, "current" => $member["credit2"] );
		}
		// code by laojie 2019-8-24
		$bankpay = array( "success" => false );
		if( isset($set["pay"]) && $set["pay"]["bankpay"] == 1 ) 
		{
			$bankpay = array( "success" => true);
		}
		// end code
		$order["price"] = floatval($order["price"]);
		$plugincoupon = com("coupon");
		if( $plugincoupon ) 
		{
			$coupondata = $plugincoupon->getCouponByDataID($order["couponid"]);
			if( $coupondata["used"] == 1 ) 
			{
				$this->message("出错了,此优惠券已经被使用过,请刷新页面重新下单");
			}
		}
		if( empty($order["price"]) && !$credit["success"] ) 
		{
			header("location: " . mobileUrl("order/pay/complete", array( "id" => $order["id"], "type" => "credit", "ordersn" => $order["ordersn"] )));
			exit();
		}
		load()->model("payment");
		$setting = uni_setting($_W["uniacid"], array( "payment" ));
		$sec = m("common")->getSec();
		$sec = iunserializer($sec["sec"]);
		$wechat = array( "success" => false );
		$jie = intval($_GPC["jie"]);
		if( is_weixin() ) 
		{
			$params = array( );
			$params["tid"] = trim($log["tid"]);
			if( !empty($order["ordersn2"]) ) 
			{
				$var = sprintf("%02d", trim($order["ordersn2"]));
				if( 100 <= $var ) 
				{
					$var = $var + 1;
					pdo_update("ewei_shop_order", array( "ordersn2" => $var ), array( "id" => $orderid, "uniacid" => $uniacid, "openid" => $openid ));
				}
				$params["tid"] .= "GJ" . $var;
			}
			$params["user"] = trim($openid);
			$params["fee"] = $order["price"];
			if( !empty($ispeerpay) ) 
			{
				$params["fee"] = $peerprice;
				$params["tid"] = trim($params["tid"]) . $member["id"] . str_replace(".", "", $params["fee"]);
				@session_start();
				$_SESSION["peerpaytid"] = trim($params["tid"]);
			}
			$params["title"] = trim($param_title);
			if( isset($set["pay"]) && $set["pay"]["weixin"] == 1 && $jie !== 1 && !empty($set["pay"]["weixin_id"]) ) 
			{
				$options = array( );
				if( is_array($setting["payment"]["wechat"]) && $setting["payment"]["wechat"]["switch"] ) 
				{
					load()->model("payment");
					$setting = uni_setting($_W["uniacid"], array( "payment" ));
					if( is_array($setting["payment"]) ) 
					{
						$options = $setting["payment"]["wechat"];
						$options["appid"] = $_W["account"]["key"];
						$options["secret"] = $_W["account"]["secret"];
					}
				}
				$wechat = m("common")->wechat_build($params, $options, 0);
				if( !is_error($wechat) ) 
				{
					$wechat["success"] = true;
					if( !empty($wechat["code_url"]) ) 
					{
						$wechat["weixin_jie"] = true;
					}
					else 
					{
						$wechat["weixin"] = true;
					}
				}
			}
			if( isset($set["pay"]) && $set["pay"]["weixin_jie"] == 1 && !$wechat["success"] || $jie === 1 ) 
			{
				if( !empty($order["ordersn2"]) ) 
				{
					$params["tid"] = $params["tid"] . "_B";
				}
				else 
				{
					$params["tid"] = $params["tid"] . "_borrow";
				}
				$options = array( );
				$options["appid"] = trim($sec["appid"]);
				$options["mchid"] = trim($sec["mchid"]);
				$options["apikey"] = trim($sec["apikey"]);
				if( !empty($set["pay"]["weixin_jie_sub"]) && !empty($sec["sub_secret_jie_sub"]) ) 
				{
					$wxuser = m("member")->wxuser($sec["sub_appid_jie_sub"], $sec["sub_secret_jie_sub"]);
					$params["openid"] = trim($wxuser["openid"]);
				}
				else 
				{
					if( !empty($sec["secret"]) ) 
					{
						$wxuser = m("member")->wxuser($sec["appid"], $sec["secret"]);
						$params["openid"] = trim($wxuser["openid"]);
					}
				}
				$wechat = m("common")->wechat_native_build($params, $options, 0);
				if( !is_error($wechat) ) 
				{
					$wechat["success"] = true;
					if( !empty($params["openid"]) ) 
					{
						$wechat["weixin"] = true;
					}
					else 
					{
						$wechat["weixin_jie"] = true;
					}
				}
			}
			$wechat["jie"] = $jie;
		}
		$alipay = array( "success" => false );
		if( empty($seckill_goods) && empty($ispeerpay) ) 
		{
			if( isset($set["pay"]) && $set["pay"]["alipay"] == 1 && is_array($setting["payment"]["alipay"]) && ($setting["payment"]["alipay"]["switch"] || $setting["payment"]["alipay"]["pay_switch"]) ) 
			{
				$params = array( );
				$params["tid"] = trim($log["tid"]);
				$params["user"] = trim($_W["openid"]);
				$params["fee"] = $order["price"];
				$params["title"] = trim($param_title);
				load()->func("communication");
				load()->model("payment");
				$setting = uni_setting($_W["uniacid"], array( "payment" ));
				if( is_array($setting["payment"]) ) 
				{
					$options = $setting["payment"]["alipay"];
					$alipay = m("common")->alipay_build($params, $options, 0, $_W["openid"]);
					if( !empty($alipay["url"]) ) 
					{
						$alipay["url"] = urlencode($alipay["url"]);
						$alipay["success"] = true;
					}
				}
			}
			list(, $payment) = m("common")->public_build();
			if( $payment["type"] == "4" ) 
			{
				$params = array( "service" => "pay.alipay.native", "body" => trim($param_title), "out_trade_no" => trim($log["tid"]), "total_fee" => $order["price"] );
				if( !empty($order["ordersn2"]) ) 
				{
					$params["out_trade_no"] = $log["tid"] . "_B";
				}
				else 
				{
					$params["out_trade_no"] = $log["tid"] . "_borrow";
				}
				$AliPay = m("pay")->build($params, $payment, 0);
				if( !empty($AliPay) && !is_error($AliPay) ) 
				{
					$alipay["url"] = urlencode($AliPay["code_url"]);
					$alipay["success"] = true;
				}
			}
			if( !empty($order["addressid"]) ) 
			{
				$cash = array( "success" => $order["cash"] == 1 && isset($set["pay"]) && $set["pay"]["cash"] == 1 && $order["isverify"] == 0 && $order["isvirtual"] == 0 );
			}
			$haveverifygood = m("order")->checkhaveverifygoods($orderid);
		}
		else 
		{
			$peerPaySwi = false;
			$cash = array( "success" => false );
		}
		$payinfo = array( "orderid" => $orderid, "ordersn" => trim($log["tid"]), "credit" => $credit, "alipay" => $alipay, "wechat" => $wechat, "cash" => $cash, "money" => $order["price"] );
		if( is_h5app() ) 
		{
			$payinfo = array( "wechat" => (!empty($sec["app_wechat"]["merchname"]) && !empty($set["pay"]["app_wechat"]) && !empty($sec["app_wechat"]["appid"]) && !empty($sec["app_wechat"]["appsecret"]) && !empty($sec["app_wechat"]["merchid"]) && !empty($sec["app_wechat"]["apikey"]) && 0 < $order["price"] ? true : false), "alipay" => false, "mcname" => trim($sec["app_wechat"]["merchname"]), "aliname" => (empty($_W["shopset"]["shop"]["name"]) ? trim($sec["app_wechat"]["merchname"]) : trim($_W["shopset"]["shop"]["name"])), "ordersn" => trim($log["tid"]), "money" => $order["price"], "attach" => $_W["uniacid"] . ":0", "type" => 0, "orderid" => $orderid, "credit" => $credit, "cash" => $cash );
			if( !empty($order["ordersn2"]) ) 
			{
				$var = sprintf("%02d", trim($order["ordersn2"]));
				$payinfo["ordersn"] .= "GJ" . $var;
			}
			if( !empty($set["pay"]["app_alipay"]) && (!empty($sec["app_alipay"]["public_key"]) || !empty($sec["app_alipay"]["public_key_rsa2"])) ) 
			{
				$payinfo["alipay"] = true;
			}
		}
		if( p("seckill") ) 
		{
			foreach( $seckill_goods as $data ) 
			{
				plugin_run("seckill::getSeckill", $data["goodsid"], $data["optionid"], true, trim($_W["openid"]));
			}
		}
		include($this->template());
	}
	public function orderstatus() 
	{
		global $_W;
		global $_GPC;
		$uniacid = $_W["uniacid"];
		$orderid = intval($_GPC["id"]);
		$order = pdo_fetch("select status from " . tablename("ewei_shop_order") . " where id=:id and uniacid=:uniacid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid ));
		if( 1 <= $order["status"] ) 
		{
			@session_start();
			$_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"] = 1;
			show_json(1);
		}
		show_json(0);
	}
	public function complete() 
	{
		global $_W;
		global $_GPC;
		$orderid = intval($_GPC["id"]);
		$uniacid = $_W["uniacid"];
		$openid = $_W["openid"];
		$gpc_ordersn = (empty($_GPC["ordersn"]) ? $_GPC["ordersn"] : str_replace(array( "_borrow", "_B" ), "", $_GPC["ordersn"]));
		$ispeerpay = m("order")->checkpeerpay($orderid);
      	
		if( !empty($ispeerpay) ) 
		{
			$_SESSION["peerpay"] = $ispeerpay["id"];
			$peerpay = $_GPC["peerpay"];
			$peerpay = floatval(str_replace(",", "", $peerpay));
			if( $ispeerpay["peerpay_type"] == 0 && $ispeerpay["peerpay_realprice"] != $peerpay ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "参数错误");
				}
				else 
				{
					$this->message("参数错误", mobileUrl("order"));
				}
			}
			else 
			{
				if( $ispeerpay["peerpay_type"] == 1 && !empty($ispeerpay["peerpay_selfpay"]) && $ispeerpay["peerpay_selfpay"] < $peerpay && 0 < floatval($ispeerpay["peerpay_selfpay"]) ) 
				{
					if( $_W["ispost"] ) 
					{
						show_json(0, "参数错误");
					}
					else 
					{
						$this->message("参数错误", mobileUrl("order"));
					}
				}
			}
			if( $peerpay <= 0 ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "参数错误");
				}
				else 
				{
					$this->message("参数错误", mobileUrl("order"));
				}
			}
			$openid = pdo_fetchcolumn("select openid from " . tablename("ewei_shop_order") . " where id=:orderid and uniacid=:uniacid limit 1", array( ":orderid" => $orderid, ":uniacid" => $uniacid ));
			$peerpay_info = (double) pdo_fetchcolumn("select SUM(price) price from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $ispeerpay["id"] ));
		}
		else 
		{
			$_SESSION["peerpay"] = NULL;
		}
		if( is_h5app() && empty($orderid) ) 
		{
			if( strexists($gpc_ordersn, "GJ") ) 
			{
				$ordersns = explode("GJ", $gpc_ordersn);
				$ordersn = $ordersns[0];
			}
			else 
			{
				$ordersn = $gpc_ordersn;
			}
			$ordersn = rtrim($ordersn, "TR");
			$orderid = pdo_fetchcolumn("select id from " . tablename("ewei_shop_order") . " where ordersn=:ordersn and uniacid=:uniacid and openid=:openid limit 1", array( ":ordersn" => $ordersn, ":uniacid" => $uniacid, ":openid" => $openid ));
		}
		if( empty($orderid) ) 
		{
			if( $_W["ispost"] ) 
			{
				show_json(0, "参数错误");
			}
			else 
			{
				$this->message("参数错误", mobileUrl("order"));
			}
		}
		$set = m("common")->getSysset(array( "shop", "pay" ));
		$set["pay"]["weixin"] = (!empty($set["pay"]["weixin_sub"]) ? 1 : $set["pay"]["weixin"]);
		$set["pay"]["weixin_jie"] = (!empty($set["pay"]["weixin_jie_sub"]) ? 1 : $set["pay"]["weixin_jie"]);
		$member = m("member")->getMember($openid, true);
		$order = pdo_fetch("select * from " . tablename("ewei_shop_order") . " where id=:id and uniacid=:uniacid and openid=:openid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid, ":openid" => $openid ));
		$go_flag = 0;
		if( empty($order["istrade"]) && 1 <= $order["status"] ) 
		{
			$go_flag = 1;
		}
		if( !empty($order["istrade"]) && (1 < $order["status"] || $order["status"] == 1 && $order["tradestatus"] == 2) ) 
		{
			$go_flag = 1;
		}
		if( $go_flag == 1 ) 
		{
			$pay_result = true;
			if( $_W["ispost"] ) 
			{
				$_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"] = 1;
				show_json(1, array( "result" => $pay_result ));
			}
			else 
			{
				header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
				exit();
			}
		}
		if( 0 < $order["ispackage"] ) 
		{
			$package = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_package") . " WHERE uniacid = " . $uniacid . " and id = " . $order["packageid"] . " ");
			if( empty($package) ) 
			{
				show_json(0, "未找到套餐！");
			}
			if( time() < $package["starttime"] ) 
			{
				show_json(0, "套餐活动未开始，请耐心等待！");
			}
			if( $package["endtime"] < time() ) 
			{
				show_json(0, "套餐活动已结束，谢谢您的关注，请您浏览其他套餐或商品！");
			}
		}
		if( empty($order) ) 
		{
			if( $_W["ispost"] ) 
			{
				show_json(0, "订单未找到");
			}
			else 
			{
				$this->message("订单未找到", mobileUrl("order"));
			}
		}
		$type = $_GPC["type"];
		if( !in_array($type, array( "wechat", "alipay", "credit", "cash" )) ) 
		{
			if( $_W["ispost"] ) 
			{
				show_json(0, "未找到支付方式");
			}
			else 
			{
				$this->message("未找到支付方式", mobileUrl("order"));
			}
		}
		$log = pdo_fetch("SELECT * FROM " . tablename("core_paylog") . " WHERE `uniacid`=:uniacid AND `module`=:module AND `tid`=:tid limit 1", array( ":uniacid" => $uniacid, ":module" => "ewei_shopv2", ":tid" => $order["ordersn"] ));
		if( empty($log) && empty($ispeerpay) ) 
		{
			if( $_W["ispost"] ) 
			{
				show_json(0, "支付出错,请重试!");
			}
			else 
			{
				$this->message("支付出错,请重试!", mobileUrl("order"));
			}
		}
		if( (empty($order["isnewstore"]) || empty($order["storeid"])) && empty($order["istrade"]) ) 
		{
			$order_goods = pdo_fetchall("select og.id,g.title, og.goodsid,og.optionid,g.total as stock,og.total as buycount,g.status,g.deleted,g.maxbuy,g.usermaxbuy,g.istime,g.timestart,g.timeend,g.buylevels,g.buygroups,g.totalcnf,og.seckill from  " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on og.goodsid = g.id " . " where og.orderid=:orderid and og.uniacid=:uniacid ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $orderid ));
			foreach( $order_goods as $data ) 
			{
				if( empty($data["status"]) || !empty($data["deleted"]) ) 
				{
					if( $_W["ispost"] ) 
					{
						show_json(0, $data["title"] . "<br/> 已下架!");
					}
					else 
					{
						$this->message($data["title"] . "<br/> 已下架!", mobileUrl("order"));
					}
				}
				$unit = (empty($data["unit"]) ? "件" : $data["unit"]);
				$seckillinfo = plugin_run("seckill::getSeckill", $data["goodsid"], $data["optionid"], true, $_W["openid"]);
				if( $data["seckill"] && (empty($seckillinfo) || $seckillinfo["status"] != 0 || $seckillinfo["endtime"] < time()) ) 
				{
					if( $_W["ispost"] ) 
					{
						show_json(0, $data["title"] . "<br/> 秒杀已结束，无法支付!");
					}
					else 
					{
						$this->message($data["title"] . "<br/> 秒杀已结束，无法支付!", mobileUrl("order"));
					}
				}
				if( $seckillinfo && $seckillinfo["status"] == 0 || !empty($ispeerpay) ) 
				{
				}
				else 
				{
					if( 0 < $data["minbuy"] && $data["buycount"] < $data["minbuy"] ) 
					{
						if( $_W["ispost"] ) 
						{
							show_json(0, $data["title"] . "<br/> " . $data["min"] . $unit . "起售!", mobileUrl("order"));
						}
						else 
						{
							$this->message($data["title"] . "<br/> " . $data["min"] . $unit . "起售!", mobileUrl("order"));
						}
					}
					if( 0 < $data["maxbuy"] && $data["maxbuy"] < $data["buycount"] ) 
					{
						if( $_W["ispost"] ) 
						{
							show_json(0, $data["title"] . "<br/> 一次限购 " . $data["maxbuy"] . $unit . "!");
						}
						else 
						{
							$this->message($data["title"] . "<br/> 一次限购 " . $data["maxbuy"] . $unit . "!", mobileUrl("order"));
						}
					}
					if( 0 < $data["usermaxbuy"] ) 
					{
						$order_goodscount = pdo_fetchcolumn("select ifnull(sum(og.total),0)  from " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_order") . " o on og.orderid=o.id " . " where og.goodsid=:goodsid and  o.status>=1 and o.openid=:openid  and og.uniacid=:uniacid ", array( ":goodsid" => $data["goodsid"], ":uniacid" => $uniacid, ":openid" => $openid ));
						if( $data["usermaxbuy"] <= $order_goodscount ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/> 最多限购 " . $data["usermaxbuy"] . $unit);
							}
							else 
							{
								$this->message($data["title"] . "<br/> 最多限购 " . $data["usermaxbuy"] . $unit, mobileUrl("order"));
							}
						}
					}
					if( $data["istime"] == 1 ) 
					{
						if( time() < $data["timestart"] ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/> 限购时间未到!");
							}
							else 
							{
								$this->message($data["title"] . "<br/> 限购时间未到!", mobileUrl("order"));
							}
						}
						if( $data["timeend"] < time() ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/> 限购时间已过!");
							}
							else 
							{
								$this->message($data["title"] . "<br/> 限购时间已过!", mobileUrl("order"));
							}
						}
					}
					if( $data["buylevels"] != "" ) 
					{
						$buylevels = explode(",", $data["buylevels"]);
						if( !in_array($member["level"], $buylevels) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, "您的会员等级无法购买<br/>" . $data["title"] . "!");
							}
							else 
							{
								$this->message("您的会员等级无法购买<br/>" . $data["title"] . "!", mobileUrl("order"));
							}
						}
					}
					if( $member["groupid"] == "" ) 
					{
						$groupid = array( );
					}
					else 
					{
						$groupid = explode(",", $member["groupid"]);
					}
					if( $data["buygroups"] != "" ) 
					{
						if( empty($groupid) ) 
						{
							$groupid[] = 0;
						}
						$buygroups = explode(",", $data["buygroups"]);
						if( !in_array($member["groupid"], $buygroups) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, "您所在会员组无法购买<br/>" . $data["title"] . "!");
							}
							else 
							{
								$this->message("您所在会员组无法购买<br/>" . $data["title"] . "!", mobileUrl("order"));
							}
						}
					}
				}
				if( $data["totalcnf"] == 1 ) 
				{
					if( !empty($data["optionid"]) ) 
					{
						$option = pdo_fetch("select id,title,marketprice,goodssn,productsn,stock,`virtual` from " . tablename("ewei_shop_goods_option") . " where id=:id and goodsid=:goodsid and uniacid=:uniacid  limit 1", array( ":uniacid" => $uniacid, ":goodsid" => $data["goodsid"], ":id" => $data["optionid"] ));
						if( !empty($option) && $option["stock"] != -1 && empty($option["stock"]) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/>" . $option["title"] . " 库存不足!");
							}
							else 
							{
								$this->message($data["title"] . "<br/>" . $option["title"] . " 库存不足!", mobileUrl("order"));
							}
						}
					}
					else 
					{
						if( $data["stock"] != -1 && empty($data["stock"]) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/>库存不足!");
							}
							else 
							{
								$this->message($data["title"] . "<br/>库存不足!", mobileUrl("order"));
							}
						}
					}
				}
			}
			if( !$seckillinfo ) 
			{
				$ret = m("order")->CheckoodsStock($orderid, 1);
				if( !$ret ) 
				{
					show_json(0, "您当前下单商品库存不足,请重新下单");
				}
			}
		}
		else 
		{
			if( p("newstore") ) 
			{
				$sql = "select og.id,g.title, og.goodsid,og.optionid,ng.stotal as stock,og.total as buycount,ng.gstatus as status,ng.deleted,g.maxbuy,g.usermaxbuy,g.istime,g.timestart,g.timeend,g.buylevels,g.buygroups,g.totalcnf,og.seckill,og.storeid " . " from " . tablename("ewei_shop_order_goods") . " og left join  " . tablename("ewei_shop_goods") . " g  on g.id=og.goodsid and g.uniacid=og.uniacid" . " inner join " . tablename("ewei_shop_newstore_goods") . " ng on g.id=ng.goodsid   " . " where og.orderid=:orderid and og.uniacid=:uniacid and ng.storeid=:storeid";
				$order_goods = pdo_fetchall($sql, array( ":uniacid" => $uniacid, ":orderid" => $orderid, ":storeid" => $order["storeid"] ));
				foreach( $order_goods as $data ) 
				{
					if( empty($data["status"]) || !empty($data["deleted"]) ) 
					{
						if( $_W["ispost"] ) 
						{
							show_json(0, $data["title"] . "<br/> 已下架!");
						}
						else 
						{
							$this->message($data["title"] . "<br/> 已下架!", mobileUrl("order"));
						}
					}
					$unit = (empty($data["unit"]) ? "件" : $data["unit"]);
					if( 0 < $data["minbuy"] && $data["buycount"] < $data["minbuy"] ) 
					{
						if( $_W["ispost"] ) 
						{
							show_json(0, $data["title"] . "<br/> " . $data["min"] . $unit . "起售!", mobileUrl("order"));
						}
						else 
						{
							$this->message($data["title"] . "<br/> " . $data["min"] . $unit . "起售!", mobileUrl("order"));
						}
					}
					if( 0 < $data["maxbuy"] && $data["maxbuy"] < $data["buycount"] ) 
					{
						if( $_W["ispost"] ) 
						{
							show_json(0, $data["title"] . "<br/> 一次限购 " . $data["maxbuy"] . $unit . "!");
						}
						else 
						{
							$this->message($data["title"] . "<br/> 一次限购 " . $data["maxbuy"] . $unit . "!", mobileUrl("order"));
						}
					}
					if( 0 < $data["usermaxbuy"] ) 
					{
						$order_goodscount = pdo_fetchcolumn("select ifnull(sum(og.total),0)  from " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_order") . " o on og.orderid=o.id " . " where og.goodsid=:goodsid and  o.status>=1 and o.openid=:openid  and og.uniacid=:uniacid ", array( ":goodsid" => $data["goodsid"], ":uniacid" => $uniacid, ":openid" => $openid ));
						if( $data["usermaxbuy"] <= $order_goodscount ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, $data["title"] . "<br/> 最多限购 " . $data["usermaxbuy"] . $unit);
							}
							else 
							{
								$this->message($data["title"] . "<br/> 最多限购 " . $data["usermaxbuy"] . $unit, mobileUrl("order"));
							}
						}
					}
					if( $data["buylevels"] != "" ) 
					{
						$buylevels = explode(",", $data["buylevels"]);
						if( !in_array($member["level"], $buylevels) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, "您的会员等级无法购买<br/>" . $data["title"] . "!");
							}
							else 
							{
								$this->message("您的会员等级无法购买<br/>" . $data["title"] . "!", mobileUrl("order"));
							}
						}
					}
					if( $member["groupid"] == "" ) 
					{
						$groupid = array( );
					}
					else 
					{
						$groupid = explode(",", $member["groupid"]);
					}
					if( $data["buygroups"] != "" ) 
					{
						if( empty($groupid) ) 
						{
							$groupid[] = 0;
						}
						$buygroups = explode(",", $data["buygroups"]);
						$intersect = array_intersect($groupid, $buygroups);
						if( empty($intersect) ) 
						{
							if( $_W["ispost"] ) 
							{
								show_json(0, "您所在会员组无法购买<br/>" . $data["title"] . "!");
							}
							else 
							{
								$this->message("您所在会员组无法购买<br/>" . $data["title"] . "!", mobileUrl("order"));
							}
						}
					}
					if( $data["totalcnf"] == 1 ) 
					{
						if( !empty($data["optionid"]) ) 
						{
							$option = pdo_fetch("select id,marketprice,stock from " . tablename("ewei_shop_newstore_goods_option") . " where optionid=:optionid and goodsid=:goodsid and uniacid=:uniacid and storeid=:storeid  limit 1", array( ":uniacid" => $uniacid, ":goodsid" => $data["goodsid"], ":optionid" => $data["optionid"], ":storeid" => $data["storeid"] ));
							if( !empty($option) && $option["stock"] != -1 && empty($option["stock"]) ) 
							{
								if( $_W["ispost"] ) 
								{
									show_json(0, $data["title"] . "<br/>" . $option["title"] . " 库存不足!");
								}
								else 
								{
									$this->message($data["title"] . "<br/>" . $option["title"] . " 库存不足!", mobileUrl("order"));
								}
							}
						}
						else 
						{
							if( $data["stock"] != -1 && empty($data["stock"]) ) 
							{
								if( $_W["ispost"] ) 
								{
									show_json(0, $data["title"] . "<br/>库存不足!");
								}
								else 
								{
									$this->message($data["title"] . "<br/>库存不足!", mobileUrl("order"));
								}
							}
						}
					}
				}
			}
			else 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "门店歇业,不能付款!");
				}
				else 
				{
					$this->message("门店歇业,不能付款!", mobileUrl("order"));
				}
			}
		}
		if( $type == "cash" && empty($ispeerpay) ) 
		{
			if( empty($set["pay"]["cash"]) ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "未开启货到付款!");
				}
				else 
				{
					$this->message("未开启货到付款", mobileUrl("order"));
				}
			}
			m("order")->setOrderPayType($order["id"], 3, $gpc_ordersn);
			$ret = array( );
			$ret["result"] = "success";
			$ret["type"] = "cash";
			$ret["from"] = "return";
			$ret["tid"] = $log["tid"];
			$ret["user"] = $order["openid"];
			$ret["fee"] = $order["price"];
			$ret["weid"] = $_W["uniacid"];
			$ret["uniacid"] = $_W["uniacid"];
			$pay_result = m("order")->payResult($ret);
			pdo_update("ewei_shop_order", array( "cashtime" => time() ), array( "id" => $order["id"] ));
			m("notice")->sendOrderMessage($order["id"]);
			@session_start();
			$_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"] = 1;
			if( $_W["ispost"] ) 
			{
				show_json(1, array( "result" => $pay_result ));
			}
			else 
			{
				header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
			}
		}
		if( !empty($ispeerpay) ) 
		{
			$total = $peerpay_info + $peerpay;
			if( $ispeerpay["peerpay_realprice"] <= $total ) 
			{
				unset($_SESSION["peerpay"]);
			}
			if( $ispeerpay["peerpay_realprice"] < $total ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "不能超付");
				}
				else 
				{
					$this->message("不能超付");
				}
			}
			$log["fee"] = $peerpay;
			$openid = $_W["openid"];
			$member = m("member")->getMember($openid, true);
		}
		$ps = array( );
		$ps["tid"] = $log["tid"];
		$ps["user"] = $openid;
		$ps["fee"] = $log["fee"];
		$ps["title"] = $log["title"];
		// code by laojie 2019-8-24
		if( $type == "bankpay" )
		{
			if( $_W["ispost"] ) 
			{
				show_json(1, array( "result" => $pay_result ));
			}
			else 
			{
				echo 'bbb';die;
				header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
			}
			exit();
		} 
		// end code
		if( $type == "credit" ) 
		{
			$open_redis = function_exists("redis") && !is_error(redis());
			if( $open_redis ) 
			{
				$redis_key = (string) $_W["uniacid"] . "_member_order__pay_" . $orderid;
				$redis = redis();
				if( !is_error($redis) ) 
				{
					if( $redis->get($redis_key) ) 
					{
						show_json(0, "请勿重复点击");
					}
					$redis->setex($redis_key, 1, time());
				}
			}
			if( empty($set["pay"]["credit"]) && 0 < $ps["fee"] ) 
			{
				if( $_W["ispost"] )
				{
					show_json(0, "未开启余额支付!");
				}
				else
				{
					$this->message("未开启余额支付", mobileUrl("order"));
				}
			}
			if( $ps["fee"] < 0 ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "金额错误");
				}
				else 
				{
					$this->message("金额错误", mobileUrl("order"));
				}
			}
			$credits = m("member")->getCredit($openid, "credit2");
			if( $credits < $ps["fee"] ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, "余额不足,请充值");
				}
				else 
				{
					$this->message("余额不足,请充值", mobileUrl("order"));
				}
			}
			$fee = floatval($ps["fee"]);
			$result = m("member")->setCredit($openid, "credit2", 0 - $fee, array( $_W["member"]["uid"], $_W["shopset"]["shop"]["name"] . "消费" . $fee ));
			if( is_error($result) ) 
			{
				if( $_W["ispost"] ) 
				{
					show_json(0, $result["message"]);
				}
				else 
				{
					$this->message($result["message"], mobileUrl("order"));
				}
			}
			$record = array( );
			$record["status"] = "1";
			$record["type"] = "cash";
			pdo_update("core_paylog", $record, array( "plid" => $log["plid"] ));
			m("order")->setOrderPayType($order["id"], 1, $gpc_ordersn);
			$ret = array( );
			$ret["result"] = "success";
			$ret["type"] = $log["type"];
			$ret["from"] = "return";
			$ret["tid"] = $log["tid"];
			$ret["user"] = $log["openid"];
			$ret["fee"] = $log["fee"];
			$ret["weid"] = $log["weid"];
			$ret["uniacid"] = $log["uniacid"];
			@session_start();
			$_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"] = 1;
			if( !empty($ispeerpay) ) 
			{
				$peerheadimg = m("member")->getInfo($member["openid"]);
				if( empty($peerheadimg["avatar"]) ) 
				{
					$peerheadimg["avatar"] = "http://of6odhdq1.bkt.clouddn.com/d7fd47dc6163ec00abfe644ab3c33ac6.jpg";
				}
				m("order")->peerStatus(array( "pid" => $ispeerpay["id"], "uid" => $member["id"], "uname" => $member["nickname"], "usay" => "", "price" => $log["fee"], "createtime" => time(), "headimg" => $peerheadimg["avatar"], "openid" => $peerheadimg["openid"], "usay" => trim($_GPC["peerpaymessage"]) ));
			}
			$pay_result = m("order")->payResult($ret);
			$open_redis = function_exists("redis") && !is_error(redis());
			if( $open_redis ) 
			{
				$redis_key = (string) $_W["uniacid"] . "_peerpay_order__pay_" . $ispeerpay["id"];
				$redis = redis();
				$redis->delete($redis_key);
			}
          
          	// code by 鱼河岸小店
          	// $this->yh_thrid_cost_price($orderid);
          	m("order")->yh_thrid_cost_price($orderid);
          	// code by laojie
			p("commission")->downCommission($orderid);
          	// code by laojie
            $set = p("commission")->getSet();
            $leveltype = intval($set["leveltype"]);
            if ($leveltype == 11) {
                p("commission")->upgradeLevel11($openid);
            }
            // end code
			if( $_W["ispost"] ) 
			{
				show_json(1, array( "result" => $pay_result ));
			}
			else 
			{
				header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
			}
		}
		else 
		{
			if( $type == "wechat" ) 
			{
				if( !is_weixin() && empty($_W["shopset"]["wap"]["open"]) ) 
				{
					if( $_W["ispost"] ) 
					{
						show_json(0, (is_h5app() ? "APP正在维护" : "非微信环境!"));
					}
					else 
					{
						$this->message((is_h5app() ? "APP正在维护" : "非微信环境!"), mobileUrl("order"));
					}
				}
				if( empty($set["pay"]["weixin"]) && empty($set["pay"]["weixin_jie"]) && is_weixin() || empty($set["pay"]["app_wechat"]) && is_h5app() ) 
				{
					if( $_W["ispost"] ) 
					{
						show_json(0, "未开启微信支付!");
					}
					else 
					{
						$this->message("未开启微信支付!", mobileUrl("order"));
					}
				}
				$ordersn = $order["ordersn"];
				if( !empty($order["ordersn2"]) ) 
				{
					$ordersn .= "GJ" . sprintf("%02d", $order["ordersn2"]);
				}
				if( !empty($ispeerpay) ) 
				{
					$payquery = m("finance")->isWeixinPay($_SESSION["peerpaytid"], $order["price"], (is_h5app() ? true : false));
					$payquery_jie = m("finance")->isWeixinPayBorrow($_SESSION["peerpaytid"], $order["price"]);
				}
				else 
				{
					$payquery = m("finance")->isWeixinPay($ordersn, $order["price"], (is_h5app() ? true : false));
					$payquery_jie = m("finance")->isWeixinPayBorrow($ordersn, $order["price"]);
				}
				if( !is_error($payquery) || !is_error($payquery_jie) || !empty($ispeerpay) ) 
				{
					$record = array( );
					$record["status"] = "1";
					$record["type"] = "wechat";
					pdo_update("core_paylog", $record, array( "plid" => $log["plid"] ));
					m("order")->setOrderPayType($order["id"], 21, $gpc_ordersn);
					if( is_h5app() ) 
					{
						pdo_update("ewei_shop_order", array( "apppay" => 1 ), array( "id" => $order["id"] ));
					}
					$ret = array( );
					$ret["result"] = "success";
					$ret["type"] = "wechat";
					$ret["from"] = "return";
					$ret["tid"] = $log["tid"];
					$ret["user"] = $log["openid"];
					$ret["fee"] = $log["fee"];
					$ret["weid"] = $log["weid"];
					$ret["uniacid"] = $log["uniacid"];
					$ret["deduct"] = intval($_GPC["deduct"]) == 1;
					if( !empty($ispeerpay) ) 
					{
						$udata = array( );
						$udata["usay"] = trim($_GPC["peerpaymessage"]);
						pdo_update("ewei_shop_order_peerpay_payinfo", $udata, array( "pid" => $ispeerpay["id"], "openid" => $_W["openid"] ));
					}
					$pay_result = m("order")->payResult($ret);
					@session_start();
					$_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"] = 1;
					if( $_W["ispost"] ) 
					{
						show_json(1, array( "result" => $pay_result ));
					}
					else 
					{
						header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
					}
					exit();
				}
				if( $_W["ispost"] ) 
				{
					show_json(0, "支付出错,请重试!");
				}
				else 
				{
					$this->message("支付出错,请重试!", mobileUrl("order"));
				}
			}
		}
	}
	// code by laojie 2019-8-24
	public function bankpaysuccess() 
	{
		global $_GPC;
		$data = m("common")->getSysset("pay");
		$data = $data['bankpaydata'];
		include($this->template());
	}
	// end code
	public function success() 
	{
		global $_W;
		global $_GPC;
		$openid = $_W["openid"];
		$uniacid = $_W["uniacid"];
		$member = m("member")->getMember($openid, true);
		$orderid = intval($_GPC["id"]);
        $goods = pdo_fetchall("select og.goodsid,g.selfRebate,og.price,g.title,g.thumb,og.total,g.credit,og.optionid,og.optionname as optiontitle,g.isverify,g.storeids from " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on g.id=og.goodsid " . " where og.orderid=:orderid and og.uniacid=:uniacid ", array( ":uniacid" => $uniacid, ":orderid" => $orderid ));
        $result = m('slwxkf')->selfRebate($goods,$_W['uniacid'],$orderid);
		if( empty($orderid) )
		{
			$this->message("参数错误", mobileUrl("order"), "error");
		}
		$order = pdo_fetch("select * from " . tablename("ewei_shop_order") . " where id=:id and uniacid=:uniacid and openid=:openid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid, ":openid" => $openid ));
		if ($order['paytype'] == 22){
		    $result = pdo_update('ewei_shop_order',array('status' => 1),array('id' => $orderid));
        }
		@session_start();
		if( empty($_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"]) ) 
		{
			if( empty($order["istrade"]) ) 
			{
				header("location: " . mobileUrl("order"));
			}
			else 
			{
				header("location: " . mobileUrl("newstore/norder"));
			}
			exit();
		}
		unset($_SESSION[EWEI_SHOPV2_PREFIX . "_order_pay_complete"]);
		$hasverifygood = m("order")->checkhaveverifygoods($orderid);
		$isonlyverifygoods = m("order")->checkisonlyverifygoods($orderid);
		$ispeerpay = m("order")->checkpeerpay($orderid);
		if( !empty($ispeerpay) ) 
		{
			$peerpay = floatval($_GPC["peerpay"]);
			$openid = pdo_fetchcolumn("select openid from " . tablename("ewei_shop_order") . " where id=:orderid and uniacid=:uniacid limit 1", array( ":orderid" => $orderid, ":uniacid" => $uniacid ));
			$order["price"] = $ispeerpay["realprice"];
			$peerpayuid = m("member")->getInfo($_W["openid"]);
			$peerprice = pdo_fetch("SELECT `price` FROM " . tablename("ewei_shop_order_peerpay_payinfo") . " WHERE uid = :uid ORDER BY id DESC LIMIT 1", array( ":uid" => $peerpayuid["id"] ));
			$share = false;
			if( com("coupon") ) 
			{
				$share = com("coupon")->activity((empty($peerprice) ? 0 : $peerprice["price"]));
			}
		}
		else 
		{
			if( !empty($order["istrade"]) ) 
			{
				if( $order["status"] == 1 && $order["tradestatus"] == 1 ) 
				{
					$order["price"] = $order["dowpayment"];
				}
				else 
				{
					if( $order["status"] == 1 && $order["tradestatus"] == 2 ) 
					{
						$order["price"] = $order["betweenprice"];
					}
				}
			}
			$merchid = $order["merchid"];
			$goods = pdo_fetchall("select og.goodsid,og.price,g.title,g.thumb,og.total,g.credit,og.optionid,og.optionname as optiontitle,g.isverify,g.storeids from " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on g.id=og.goodsid " . " where og.orderid=:orderid and og.uniacid=:uniacid ", array( ":uniacid" => $uniacid, ":orderid" => $orderid ));
			// code by laojie
          	if (p('team_range') && p('team_range')->status()) {
                p('team_range')->update_level($member, $order);
            }
          	// end code
          	$address = false;
			if( !empty($order["addressid"]) ) 
			{
				$address = iunserializer($order["address"]);
				if( !is_array($address) ) 
				{
					$address = pdo_fetch("select * from  " . tablename("ewei_shop_member_address") . " where id=:id limit 1", array( ":id" => $order["addressid"] ));
				}
			}
			$carrier = @iunserializer($order["carrier"]);
			if( !is_array($carrier) || empty($carrier) ) 
			{
				$carrier = false;
			}
			$store = false;
			if( !empty($order["storeid"]) ) 
			{
				if( 0 < $merchid ) 
				{
					$store = pdo_fetch("select * from  " . tablename("ewei_shop_merch_store") . " where id=:id limit 1", array( ":id" => $order["storeid"] ));
				}
				else 
				{
					$store = pdo_fetch("select * from  " . tablename("ewei_shop_store") . " where id=:id limit 1", array( ":id" => $order["storeid"] ));
				}
			}
			$stores = false;
			if( $order["isverify"] ) 
			{
				$storeids = array( );
				foreach( $goods as $g ) 
				{
					if( !empty($g["storeids"]) ) 
					{
						$storeids = array_merge(explode(",", $g["storeids"]), $storeids);
					}
				}
				if( p("newstore") && !empty($order["isnewstore"]) ) 
				{
					$stores = pdo_fetchall("select * from " . tablename("ewei_shop_store") . " where id = :id  and uniacid=:uniacid and status=1  order by displayorder desc,id desc", array( ":uniacid" => $_W["uniacid"], ":id" => $order["storeid"] ));
				}
				else 
				{
					if( empty($storeids) ) 
					{
						if( 0 < $merchid ) 
						{
							$stores = pdo_fetchall("select * from " . tablename("ewei_shop_merch_store") . " where  uniacid=:uniacid and merchid=:merchid and status=1 and type in (2,3) order by displayorder desc,id desc", array( ":uniacid" => $_W["uniacid"], ":merchid" => $merchid ));
						}
						else 
						{
							$stores = pdo_fetchall("select * from " . tablename("ewei_shop_store") . " where  uniacid=:uniacid and status=1 and `type` in (2,3)  order by displayorder desc,id desc", array( ":uniacid" => $_W["uniacid"] ));
						}
					}
					else 
					{
						if( 0 < $merchid ) 
						{
							$stores = pdo_fetchall("select * from " . tablename("ewei_shop_merch_store") . " where id in (" . implode(",", $storeids) . ") and uniacid=:uniacid and merchid=:merchid and status=1 order by displayorder desc,id desc", array( ":uniacid" => $_W["uniacid"], ":merchid" => $merchid ));
						}
						else 
						{
							$stores = pdo_fetchall("select * from " . tablename("ewei_shop_store") . " where id in (" . implode(",", $storeids) . ") and uniacid=:uniacid and status=1 order by displayorder desc,id desc", array( ":uniacid" => $_W["uniacid"] ));
						}
					}
				}
			}
			if( p("lottery") ) 
			{
				$lottery_changes = p("lottery")->check_isreward();
			}
			$share = false;
			if( com("coupon") ) 
			{
				$share = com("coupon")->activity($order["price"]);
			}
		}
		if( !empty($order["virtual"]) && !empty($order["virtual_str"]) ) 
		{
			$ordervirtual = m("order")->getOrderVirtual($order);
			$virtualtemp = pdo_fetch("SELECT linktext, linkurl FROM " . tablename("ewei_shop_virtual_type") . " WHERE id=:id AND uniacid=:uniacid LIMIT 1", array( ":id" => $order["virtual"], ":uniacid" => $_W["uniacid"] ));
		}
		$plugincoupon = com("coupon");
		if( $plugincoupon ) 
		{
			$plugincoupon->useConsumeCoupon($orderid);
		}
		if( 0 < $order["seckilldiscountprice"] && p("diypage") ) 
		{
			$diypagedata = m("common")->getPluginset("diypage");
			$diypage = p("diypage")->seckillPage($diypagedata["seckill"]);
			if( !empty($diypage) ) 
			{
				$seckill_color = $diypage["seckill_color"];
			}
		}
		//by  baile
		p("commission")->checkOrderPay($order["id"]);
		//
      	// code by laojie
		p("commission")->upgradeLevelByAgent($openid);
		// end code
		include($this->template());
	}
	protected function str($str) 
	{
		$str = str_replace("\"", "", $str);
		$str = str_replace("'", "", $str);
		return $str;
	}
	public function check() 
	{
		global $_W;
		global $_GPC;
		$orderid = intval($_GPC["id"]);
		$og_array = m("order")->checkOrderGoods($orderid);
		if( !empty($og_array["flag"]) ) 
		{
			show_json(0, $og_array["msg"]);
		}
		show_json(1);
	}
	public function message($msg, $redirect = "", $type = "") 
	{
		global $_W;
		$title = "";
		$buttontext = "";
		$message = $msg;
		if( is_array($msg) ) 
		{
			$message = (isset($msg["message"]) ? $msg["message"] : "");
			$title = (isset($msg["title"]) ? $msg["title"] : "");
			$buttontext = (isset($msg["buttontext"]) ? $msg["buttontext"] : "");
		}
		if( empty($redirect) ) 
		{
			$redirect = "javascript:history.back(-1);";
		}
		else 
		{
			if( $redirect == "close" ) 
			{
				$redirect = "javascript:WeixinJSBridge.call(\"closeWindow\")";
			}
		}
		include($this->template("_message"));
		exit();
	}
	public function peerpay() 
	{
		global $_W;
		global $_GPC;
		$openid = $_W["openid"];
		$uniacid = $_W["uniacid"];
		$orderid = intval($_GPC["id"]);
		$PeerPay = com_run("sale::getPeerPay");
		if( empty($orderid) || empty($PeerPay) ) 
		{
			header("location: " . mobileUrl("order"));
			exit();
		}
		$peerpay = (int) pdo_fetchcolumn("select orderid from " . tablename("ewei_shop_order_peerpay") . " where orderid=:id and uniacid=:uniacid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid ));
		if( !empty($peerpay) ) 
		{
			header("location: " . mobileUrl("order/pay/peerpayshare", array( "id" => $peerpay )));
			exit();
		}
		$order = pdo_fetch("select * from " . tablename("ewei_shop_order") . " where id=:id and uniacid=:uniacid limit 1", array( ":id" => $orderid, ":uniacid" => $uniacid ));
		if( 100 <= $order["ordersn2"] ) 
		{
			$order["ordersn2"] = 0;
			pdo_update("ewei_shop_order", array( "ordersn2" => 0 ), array( "id" => $orderid, "uniacid" => $uniacid ));
		}
		if( !empty($order["ordersn2"]) ) 
		{
			$this->message("改价订单不支持代付");
		}
		if( $_W["ispost"] ) 
		{
			$data = array( );
			$data["uniacid"] = $_W["uniacid"];
			$data["orderid"] = $orderid;
			$data["peerpay_type"] = (int) $_GPC["type"];
			$data["peerpay_price"] = (double) $order["price"];
			$data["peerpay_realprice"] = ($PeerPay["peerpay_price"] <= $order["price"] ? round($order["price"] - $PeerPay["peerpay_privilege"], 2) : (double) $order["price"]);
			$data["peerpay_selfpay"] = $PeerPay["self_peerpay"];
			$data["peerpay_message"] = trim($_GPC["message"]);
			$data["status"] = 0;
			$data["createtime"] = time();
			$res = pdo_insert("ewei_shop_order_peerpay", $data);
			$insert_id = pdo_insertid();
			if( $res ) 
			{
				show_json(1, array( "url" => mobileUrl("order/pay/peerpayshare", array( "id" => $orderid )) ));
			}
			show_json(0);
		}
		if( empty($order) ) 
		{
			header("location: " . mobileUrl("order"));
			exit();
		}
		if( $peerpay["isparent"] == 1 ) 
		{
			$scondition = " parentorderid=:id ";
		}
		else 
		{
			$scondition = " orderid=:id";
		}
		$goods = pdo_fetchall("SELECT g.id,og.goodsid,og.total,g.title,g.thumb,g.type,g.status,og.price,og.title as gtitle,og.optionname as optiontitle,og.optionid,op.specs,g.merchid,og.seckill,og.seckill_taskid,\r\n                og.sendtype,og.expresscom,og.expresssn,og.express,og.sendtime,og.finishtime,og.remarksend\r\n                FROM " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on og.goodsid = g.id " . " left join " . tablename("ewei_shop_goods_option") . " op on og.optionid = op.id " . " where " . $scondition . " and og.uniacid=:uniacid order by og.id asc", array( ":id" => $orderid, ":uniacid" => $uniacid ));
		$address = iunserializer($order["address"]);
		$member = m("member")->getMember($openid, true);
		$orderMember = m("member")->getMember($order["openid"], true);
		include($this->template());
	}
	public function peerpayshare() 
	{
		global $_W;
		global $_GPC;
		$peerid = intval($_GPC["id"]);
		$uniacid = $_W["uniacid"];
		$peerpay = pdo_fetch("select p.*,o.openid from " . tablename("ewei_shop_order_peerpay") . " p join " . tablename("ewei_shop_order") . " o on o.id=p.orderid where p.orderid=:id and p.uniacid=:uniacid limit 1", array( ":id" => $peerid, ":uniacid" => $uniacid ));
		if( empty($peerpay) ) 
		{
			header("location: " . mobileUrl("order"));
			exit();
		}
		if( $peerpay["openid"] !== $_W["openid"] ) 
		{
			header("location: " . mobileUrl("order/pay/peerpaydetail", array( "id" => $peerid )));
			exit();
		}
		$peerpay_info = (double) pdo_fetchcolumn("select SUM(price) price from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $peerpay["id"] ));
		$rate = round($peerpay_info / $peerpay["peerpay_realprice"] * 100, 2);
		$rate_price = round($peerpay["peerpay_realprice"] - $peerpay_info, 2);
		$member = m("member")->getMember($peerpay["openid"], true);
		$ordergoods = pdo_fetch("select * from " . tablename("ewei_shop_order_goods") . " where orderid=:id and uniacid=:uniacid limit 1", array( ":id" => $peerpay["orderid"], ":uniacid" => $uniacid ));
		$goods = pdo_fetch("select * from " . tablename("ewei_shop_goods") . " where id=:id and uniacid=:uniacid limit 1", array( ":id" => $ordergoods["goodsid"], ":uniacid" => $uniacid ));
		$_W["shopshare"] = array( "title" => "我想对你说：" . $peerpay["peerpay_message"], "imgUrl" => tomedia($goods["thumb"]), "desc" => $peerpay["peerpay_message"], "link" => mobileUrl("order/pay/peerpaydetail", array( "id" => $peerid, "mid" => $member["id"] ), 1) );
		include($this->template());
	}
	public function peerpaydetail() 
	{
		global $_W;
		global $_GPC;
		$peerid = intval($_GPC["id"]);
		$uniacid = $_W["uniacid"];
		$peerpay = pdo_fetch("select p.*,o.openid,o.address,o.isparent,o.id as oid from " . tablename("ewei_shop_order_peerpay") . " p join " . tablename("ewei_shop_order") . " o on o.id=p.orderid where p.orderid=:id and p.uniacid=:uniacid limit 1", array( ":id" => $peerid, ":uniacid" => $uniacid ));
		if( empty($peerpay) ) 
		{
			header("location: " . mobileUrl("order"));
		}
		$PeerPay = com_run("sale::getPeerPay");
		$member = m("member")->getMember($peerpay["openid"], true);
		$peerpay_info = (double) pdo_fetchcolumn("select SUM(price) price from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $peerpay["id"] ));
		$rate_price = round($peerpay["peerpay_realprice"] - $peerpay_info, 2);
		$rate = round($peerpay_info / $peerpay["peerpay_realprice"] * 100, 2);
		if( $peerpay["isparent"] == 1 ) 
		{
			$scondition = " parentorderid=:id ";
		}
		else 
		{
			$scondition = " orderid=:id";
		}
		$goods = pdo_fetchall("SELECT g.id,og.goodsid,og.total,g.title,g.thumb,g.type,g.status,og.price,og.title as gtitle,og.optionname as optiontitle,og.optionid,op.specs,g.merchid,og.seckill,og.seckill_taskid,\r\n                og.sendtype,og.expresscom,og.expresssn,og.express,og.sendtime,og.finishtime,og.remarksend\r\n                FROM " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on og.goodsid = g.id " . " left join " . tablename("ewei_shop_goods_option") . " op on og.optionid = op.id " . " where " . $scondition . " and og.uniacid=:uniacid order by og.id asc", array( ":id" => $peerpay["orderid"], ":uniacid" => $uniacid ));
		$address = (!empty($peerpay["address"]) ? iunserializer($peerpay["address"]) : "");
		if( $peerpay["peerpay_type"] == 0 ) 
		{
			$price = $peerpay["peerpay_realprice"];
		}
		else 
		{
			$message = pdo_fetchall("SELECT * FROM " . tablename("ewei_shop_order_peerpay_payinfo") . " WHERE pid = :pid ORDER BY id DESC LIMIT 3", array( ":pid" => $peerpay["id"] ));
			$price = ($peerpay["peerpay_selfpay"] < $rate_price ? $peerpay["peerpay_selfpay"] : $rate_price);
		}
		$_W["shopshare"] = array( "title" => "我想对你说：" . $peerpay["peerpay_message"], "imgUrl" => tomedia($ordergoods["thumb"]), "desc" => $peerpay["peerpay_message"], "link" => mobileUrl("order/pay/peerpaydetail", array( "id" => $peerid ), 1) );
		include($this->template());
	}
}
?>